<?php
include_once '/class_master/db_connect.class.php';
include_once '/class_master/parcourir.class.php';

class db{
	
	public function  db ()
	{
		
	}

	public function insert ($table,  $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$exec = $prepare->prepare("insert into $table values ($SqlValues) ;");
		$exec->execute();
		
	}
	

	public function update_status_machine ($table, $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$update = $prepare->query("update $table set status=1 where  id_machine = '$SqlValues' ;");
	}
	
	public function update_status_machine_0 ($table, $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$update = $prepare->query("update $table set status=0 where  id_machine = '$SqlValues' ;");
	}
	
	public function update_status_salle ($table, $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$update = $prepare->query("update $table set status=1 where  id_salle = '$SqlValues' ;");
	}
	
	public function update_status_salle_0 ($table, $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$update = $prepare->query("update $table set status=0 where  id_salle = '$SqlValues' ;");
	}
	
	public function update ($table, $SqlValues)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$update = $prepare->query("update $table set  values($SqlValues);");
	}
	
	
	public function delete ($table, $SqlContrainte)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$exec = $prepare->prepare("delete from $table  where  $SqlContrainte ;");
		$exec->execute();
	}
	

	public function select ($champ='*', $table,  $SqlContrainte, $where=' where ')
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select = $prepare->query("select ".$champ." from ".$table.$where.$SqlContrainte.";");
		return $select;
	}
	
	public function select_salle ($champ='*')
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select = $prepare->query("select ".$champ." from salle order by nom_salle asc;");
		return $select;
	}
	
	public function select_machine ($champ='*', $SqlContrainte)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select = $prepare->query("select ".$champ." from machine where id_salle = ".$SqlContrainte."  order by nom_machine asc ;");
		return $select;
	}
	
	public function select_status ($table,  $SqlContrainte)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select_status = $prepare->query("select status from ".$table." where ".$SqlContrainte. ";");
		return $select_status;
	}
	
	
	public function select_all ($champ='*',$table)
	{
		$madb = new db_connect();
		$return_var = new parcourir();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$sql = "select $champ from $table ;";
		$select_all = $prepare->query($sql);
//		$result = $return_var->get_array($select_all);
		return $select_all;
	}
	
	public function select_incident_machine ($id_incident)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select_problemes_machine = $prepare->query("
				select *  from incident where id_incident = ".$id_incident.";");
		return $select_problemes_machine;
	}
	
	public function select_id_incident_to_machine ($id_machine)
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select_id_incident_to_machine = $prepare->query("
				select id_incident  from incident where id_machine = ".$id_machine.";");
		return $select_id_incident_to_machine;
	}
	
	public function select_all_incidents_machines ()
	{
		$madb = new db_connect();
		//$madb->connexion();
		$prepare = $madb->getMaconnexion();
		$select_all_incidents_machines = $prepare->query("
				select i.debut_incident, i.fin_incident, i.nom_incident,i.status,
					 i.commantaire_incident, m.nom_machine , s.nom_salle 
						from incident as i inner join machine as m inner join salle as s on 
							i.id_machine = m.id_machine and s.id_salle = m.id_salle and 
							fin_incident = '0000-00-00 00:00:00' order by debut_incident desc ;");
		return $select_all_incidents_machines;
	}
	

	
}